/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CapaPresentacion;

import CapaLogica.Gestor;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Elayner
 */
public class PantallaMecenaEditar extends javax.swing.JFrame {
    
    private Gestor gestor=new Gestor();
    ArrayList<TreeMap> listaMecenados;
    private Date fechaActual=new Date();

    

    /**
     * Creates new form PantallaMecenaAgregar
     */
    public PantallaMecenaEditar() {
        initComponents();
    }
    
    public PantallaMecenaEditar(int pidMec) throws Exception {
        initComponents();
        loadScreenMecena(pidMec);
        fec_nac_mec.setMaxSelectableDate(fechaActual);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        id_mec = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        nom_mec = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        pais_mec = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        ciud_mec = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        fec_nac_mec = new com.toedter.calendar.JDateChooser();
        btn_actualizar = new javax.swing.JButton();
        btn_cencelar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        list_mecenados = new javax.swing.JList();
        btn_eliminar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        btn_actualziar_mz = new javax.swing.JButton();
        btn_agregar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Edtar Mecena");

        jLabel1.setText("Id:");

        id_mec.setEditable(false);
        id_mec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                id_mecActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre Completo:");

        jLabel3.setText("Pais:");

        jLabel4.setText("Ciudad:");

        jLabel5.setText("Fecha de nacimiento:");

        btn_actualizar.setText("Actualizar");
        btn_actualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_actualizarActionPerformed(evt);
            }
        });

        btn_cencelar.setText("Cancelar");
        btn_cencelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cencelarActionPerformed(evt);
            }
        });

        jLabel6.setText("Lista Mecenados");

        jScrollPane2.setViewportView(list_mecenados);

        btn_eliminar.setText("Eliminar");
        btn_eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_eliminarActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        btn_actualziar_mz.setText("Actualizar");
        btn_actualziar_mz.setActionCommand("actu");
        btn_actualziar_mz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_actualziar_mzActionPerformed(evt);
            }
        });

        btn_agregar.setText("Agregar Mecenado");
        btn_agregar.setActionCommand("borrar_mz");
        btn_agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_agregarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(18, 18, 18)
                                        .addComponent(id_mec, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(18, 18, 18)
                                        .addComponent(ciud_mec, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(18, 18, 18)
                                        .addComponent(pais_mec, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 206, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(nom_mec)
                                .addGap(10, 10, 10))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(fec_nac_mec, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addComponent(btn_actualizar)
                        .addGap(34, 34, 34)
                        .addComponent(btn_cencelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btn_actualziar_mz, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btn_eliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(btn_agregar))
                .addGap(15, 15, 15))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(24, 24, 24)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(id_mec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel6)
                                .addGap(10, 10, 10)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(nom_mec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(pais_mec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4)
                                    .addComponent(ciud_mec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5)
                                    .addComponent(fec_nac_mec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btn_agregar))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(btn_actualziar_mz)
                                .addGap(18, 18, 18)
                                .addComponent(btn_eliminar)))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btn_actualizar)
                            .addComponent(btn_cencelar))
                        .addGap(0, 31, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void id_mecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_id_mecActionPerformed
        
    }//GEN-LAST:event_id_mecActionPerformed

    private void btn_agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_agregarActionPerformed
        try {
            
            int id=Integer.parseInt(id_mec.getText());
            PantallaMecenazgoAgregar p;
            p = new PantallaMecenazgoAgregar(id,this);
            p.setVisible(true);
            
        } catch (Exception ex) {
            Logger.getLogger(PantallaMecenaEditar.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_btn_agregarActionPerformed

    private void btn_cencelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cencelarActionPerformed
       this.setVisible(false);
    }//GEN-LAST:event_btn_cencelarActionPerformed

    private void btn_actualziar_mzActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_actualziar_mzActionPerformed
        try {
            if(!list_mecenados.isSelectionEmpty()){
                int index=list_mecenados.getSelectedIndex();
                    int idArtista=(int)listaMecenados.get(index).get("id");
                    int idMec=Integer.parseInt(id_mec.getText());
                    PantallaMecenazgoActualizar p;

                    p = new PantallaMecenazgoActualizar(idMec,idArtista);
                    p.setVisible(true);
            }else{
                JOptionPane.showMessageDialog(this,(String) "Debe de seleccionar una opcion","Error",JOptionPane.ERROR_MESSAGE);
            }
                
        } catch (Exception ex) {
            Logger.getLogger(PantallaMecenaEditar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_actualziar_mzActionPerformed

    private void btn_eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_eliminarActionPerformed
        try {
            if(!list_mecenados.isSelectionEmpty()){
                int index=list_mecenados.getSelectedIndex();
                int idArtista=(int)listaMecenados.get(index).get("id");
                int idMec=Integer.parseInt(id_mec.getText());

                if(gestor.eliminarMecenazgo(idMec, idArtista))
                    this.loadScreenMecena(idMec);
            }else{
                JOptionPane.showMessageDialog(this,(String) "Debe de seleccionar una opcion","Error",JOptionPane.ERROR_MESSAGE);
            }
            
        } catch (Exception ex) {
            Logger.getLogger(PantallaMecenaEditar.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_btn_eliminarActionPerformed

    private void btn_actualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_actualizarActionPerformed
         if(validarPantalla()){
            try {
                int id=Integer.parseInt(id_mec.getText());
                String nom=nom_mec.getText();
                String pais=pais_mec.getText();
                String ciudad=ciud_mec.getText();
                String fecha=helperUI.getDateFormatConv(fec_nac_mec.getDate());
                
                if(gestor.actualizarMecenas(id, nom, pais, ciudad, fecha))
                    this.setVisible(false);
                
            } catch (Exception ex) {
                Logger.getLogger(PantallaMecenaEditar.class.getName()).log(Level.SEVERE, null, ex);
            }
             
             
             
         }else{
             JOptionPane.showMessageDialog(this,(String) "Debe de llenar los campos requeridos","Error",JOptionPane.ERROR_MESSAGE);	
        }
    }//GEN-LAST:event_btn_actualizarActionPerformed

    public void loadScreenMecena(int pidMec) throws Exception{
        TreeMap infoMecena;
        infoMecena=gestor.buscarInfoGeneralMecenas(pidMec);

        TreeMap datosMecenados;
        DefaultListModel modelo = new DefaultListModel();
        
        if(infoMecena!=null){
            id_mec.setText(infoMecena.get("id")+"");
            nom_mec.setText((String)infoMecena.get("nombre"));
            pais_mec.setText((String)infoMecena.get("pais"));
            ciud_mec.setText((String)infoMecena.get("ciudad"));
            fec_nac_mec.setDate(helperUI.getDateFormatDate((String)infoMecena.get("fecha_nac")));
            
            listaMecenados=gestor.buscarArtistasMecenadosXMecena(Integer.parseInt(id_mec.getText()));
            
            if(listaMecenados!=null){
                for(int i=0,j=1;i<listaMecenados.size();i++,j++){
                    datosMecenados=listaMecenados.get(i);
                    modelo.addElement(j+"."+datosMecenados.get("nombre"));
                }
                list_mecenados.setModel(modelo);
            }
        }
        
    }
    
    private boolean validarPantalla(){
        boolean result=true;
        
        if(id_mec.getText().length()<=0)
            result=false;
        if(nom_mec.getText().length()<=0)
            result=false;
        if(pais_mec.getText().length()<=0)
            result=false;
        if(fec_nac_mec.getDate()==null)
            result=false;
        return result;
    }
    

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaMecenaEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaMecenaEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaMecenaEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaMecenaEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaMecenaEditar().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_actualizar;
    private javax.swing.JButton btn_actualziar_mz;
    private javax.swing.JButton btn_agregar;
    private javax.swing.JButton btn_cencelar;
    private javax.swing.JButton btn_eliminar;
    private javax.swing.JTextField ciud_mec;
    private com.toedter.calendar.JDateChooser fec_nac_mec;
    private javax.swing.JTextField id_mec;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList list_mecenados;
    private javax.swing.JTextField nom_mec;
    private javax.swing.JTextField pais_mec;
    // End of variables declaration//GEN-END:variables
}
